<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="80" color="#BBBBBB"></uni-icons>
		<button type="primary" class="login-btn" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
		<text class="login-text">登录后尽享更多权益</text>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		name: "my-login",
		data() {
			return {};
		},
		computed: {
			...mapState('m_user', ['redirectInfo'])
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
			// 获取微信用户的基本信息
			getUserInfo(e) {
				// console.log(e);
				// 判断是否获取用户信息成功
				// if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权')
				// 获取用户信息成功， e.detail.userInfo 就是用户的基本信息
				console.log(e.detail.userInfo)
				if (e.detail.errMsg === 'getUserInfo:ok') {
					// this.updateUserInfo(e.detail.userInfo)
					// 调用登录方法
					this.getToken(e.detail)
				}
			},
			async getToken(info) {
				// 1.获取登录所需要的code值(微信提供的wx.login()方法)
				const [err, res] = await uni.login().catch(err => err)
				// console.log(err);
				if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败')
				console.log(res);
				// 2.准备请求参数
				const userinfoObj = {
					code: res.code,
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature
				}
				// 3.调用微信登录接口
				const {
					data: loginResult
				} = await uni.$http.post('/api/public/v1/users/wxlogin', userinfoObj)
				console.log(loginResult);
				// if (loginResult.meta.status !== 200) return uni.$showMsg('登录失败')
				// uni.$showMsg('登录成功')

				// 把登录成功后的token存进Vuex中
				// this.updateToken(loginResult.message.token)
				this.updateToken('Bearer IKUN')
				this.navigateBack()
			},
			navigateBack() {
				if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
					uni.switchTab({
						url: this.redirectInfo.from,
						complete: () => {
							this.updateRedirectInfo(null)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		height: 750rpx;
		background-color: #F8F8F8;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		position: relative;
		overflow: hidden;

		&::after {
			content: '';
			display: block;
			width: 100%;
			height: 40px;
			position: absolute;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translateY(50%);
			background-color: white;
		}

		.login-btn {
			width: 90%;
			border-radius: 100px;
			margin: 15px 0;
			background-color: #FFFFFF;
			color: gray;
			box-shadow: 0px 0px 20px #DDDDDD;

			&::after {
				border: none;
			}
		}

		.login-text {
			font-size: 12px;
			// color: #d9b8ff;
			background-image: -moz-linear-gradient(90deg, rgb(255, 199, 214), rgb(192, 173, 255));
			background-image: -webkit-linear-gradient(90deg, rgb(255, 199, 214), rgb(192, 173, 255));
			background-image: linear-gradient(90deg, rgb(255, 199, 214), rgb(192, 173, 255));
			/* 设置背景色渐变 */
			// background-image: linear-gradient(to right, #51D88F, #31BDD0);
			/* 设置背景以文字进行裁切 */
			background-clip: text;
			-webkit-background-clip: text;
			/* 设置文本颜色透明以露出后面裁切成文本形状的渐变背景 */
			color: transparent;
		}
	}
</style>
